package com.l024.framework.manager;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.l024.framework.utils.Logutils;

import java.io.IOException;

/**
 * @author wu_ming_zhi_bei
 * @date 2020/2/25 10:08
 * @Notes  多媒体播放
 */
public class MediaPlayerManager {

    //播放状态
    public static final int MEDIA_STATUS_PLAY = 0;//播放
    public static final int MEDIA_STATUS_PAUSE = 1;//暂停
    public static final int MEDIA_STATUS_STOP = 2;//停止
    //当前播放状态
    public static int MEDIA_STATUS = MEDIA_STATUS_STOP;
    private MediaPlayer mMediaPlayer;
    public static final int H_PROGRESS = 1000;

    //进度条监听
    private OnMusicProgressListener musicProgressListener;

    /**
     * 计算歌曲进度
     * 1.开始播放的时候就开启循环计算时长
     * 2.将进度时长抛出
     */
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case H_PROGRESS:{
                    if(musicProgressListener!=null){
                        //获取当前时长
                        int currentPosition = getCurrentPosition();
                        int duration = getDuration();
                        int pos = (int) (((float)currentPosition)/((float)duration)*100);
                        musicProgressListener.onProgress(currentPosition,pos);
                        //每隔一秒执行
                        handler.sendEmptyMessageDelayed(H_PROGRESS,1000);
                    }
                    break;
                }
            }
            return false;
        }
    });
    /**
     * 构造方法创建mediaPlayer
     */
    public MediaPlayerManager() {
        mMediaPlayer = new MediaPlayer();
    }

    /**
     * 开始播放
     */
    public void  startPlay(Context context, Uri path){
        try{
            //重置MediaPlayer对象
            mMediaPlayer.reset();
            //设置资源路径
            mMediaPlayer.setDataSource(context,path);
            //同步方式加载资源
            mMediaPlayer.prepare();
            //开始播放或者恢复播放
            mMediaPlayer.start();
            MEDIA_STATUS = MEDIA_STATUS_PLAY;
            //开始播放
            handler.sendEmptyMessage(H_PROGRESS);
        }catch (IOException e){
            Logutils.e(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 暂停播放
     */
    public void pausePlay(){
        //判断是否在播放才停止
        if(isPlaying()){
            mMediaPlayer.pause();
            MEDIA_STATUS = MEDIA_STATUS_PAUSE;
            handler.removeMessages(H_PROGRESS);
        }
    }

    /**
     * 停止播放
     */
    public void stopPlay(){
        mMediaPlayer.stop();
        MEDIA_STATUS = MEDIA_STATUS_STOP;
        handler.removeMessages(H_PROGRESS);
    }

    /**
     * 继续播放
     */
    public void continuePlay(){
        mMediaPlayer.start();
        MEDIA_STATUS = MEDIA_STATUS_PLAY;
        handler.sendEmptyMessage(H_PROGRESS);
    }

    /**
     * 判断当前是否播放
     */
    public boolean isPlaying(){
        return mMediaPlayer.isPlaying();
    }

    /**
     * 获取当前位置
     */
    public int getCurrentPosition(){
        return mMediaPlayer.getCurrentPosition();
    }

    /**
     * 获取音乐总时长
     */
    public int getDuration(){
        return mMediaPlayer.getDuration();
    }

    /**
     * 设置循环
     */
    public void setLooping(boolean isLooping){
        mMediaPlayer.setLooping(isLooping);
    }

    /**
     * 跳转位置
     */
    public void seekTo(int ms){
        mMediaPlayer.seekTo(ms);
    }
    /**
     * 监听播放结束
     */
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener){
        mMediaPlayer.setOnCompletionListener(listener);
    }

    /**
     * 监听播放错误
     */
    public void setOnErrorListener(MediaPlayer.OnErrorListener listener){
        mMediaPlayer.setOnErrorListener(listener);
    }

    /**
     * 监听播放进度
     */
    public void setOnProgressListener(OnMusicProgressListener listener){
        musicProgressListener = listener;
    }

    //对外接口
    public interface OnMusicProgressListener{
        void onProgress(int currentPosition,int pos);
    }

}
